<div class="layui-fluid layui-anim febs-anim" id="businesses-college" lay-title="院校信息">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="college-table-form">
                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                            <legend style="font-size: 15px; font-weight: bolder;">批量查询</legend>
                        </fieldset>
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="name" autocomplete="off" class="layui-input"
                                                   placeholder="可填写全称或简称" minlength="0" maxlength="20">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">校址</label>
                                        <div class="layui-input-inline" minlength="2" maxlength="20">
                                            <input type="text" name="address" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain
                                            table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-green-plain
                                            table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                            </div>
                        </div>
                        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                            <div class="layui-row">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain
                                            table-action" id="college_add">
                                    <i class="layui-icon layui-icon-plus-circle">新增</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain
                                            table-action" id="export">
                                    <i class="layui-icon layui-icon-plus-circle">导出全部</i>
                                </div>
                            </div>
                        </blockquote>
                    </form>
                    <table lay-filter="collegeTable" lay-data="{id: 'collegeTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="college-option">
    <a lay-event="detail"><i
            class="layui-icon febs-edit-area febs-green">&#xe7a5;</i></a>
    <a lay-event="edit"><i
            class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
    <a lay-event="del"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'form', 'table', 'febs'], function () {
        let $ = layui.jquery,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#businesses-college'),
            $query = $view.find('#query'),
            $add = $view.find('#college_add'),
            $export = $view.find('#export'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createDate', type: null},
            tableIns;

        form.render();

        initTable();

        table.on('tool(collegeTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {
                febs.modal.view('院校信息', 'college/detail/' + data.id, {
                    area: $(window).width() <= 750 ? '95%' : '660px'
                });
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除院校', '确定删除该院校？', function () {
                    deleteColleges(data.id);
                });
            }
            if (layEvent === 'edit') {
                febs.modal.open('修改院校', 'college/update/' + data.id, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#college-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });

        table.on('sort(collegeTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            let params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $add.on('click', function () {
            febs.modal.open('新增院校', 'college/add', {
                btn: ['提交', '重置'],
                area: $(window).width() <= 750 ? '95%' : '50%',
                yes: function (index, layero) {
                        let name = $('#college-add').find('#name').val();
                        let simplename = $('#college-add').find('#simplename').val();
                        $.ajax({
                            type:'get',
                            url:'college/isRepetitive',
                            data:{"name":name,"simplename":simplename},
                            success:function(res){
                                if(res){
                                    $('#college-add').find('#submit').trigger('click');
                                    // 单击之后提交按钮不可选,防止重复提交
                                    var DISABLED = 'layui-btn-disabled';
                                    // 增加样式
                                    $('#college-add').find('#submit').addClass(DISABLED);
                                    // 增加属性
                                    $('#college-add').find('#submit').attr('disabled', 'disabled');
                                }else{
                                    febs.alert.error("输入的名称或简称重复，请检查");
                                }
                            }
                        })

                },
                btn2: function () {
                    $('#college-add').find('#reset').trigger('click');
                    return false;
                }
            });
        });

        $export.on('click', function () {
            let params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            febs.download(ctx + 'college/excel', params, '院校信息表.xlsx');
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            tableIns.reload({where: getQueryParams(true), page: {curr: 1}, initSort: sortObject});
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'collegeTable',
                url: ctx + 'college/list',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'name',       title: '名称',        minWidth: 100},
                    {field: 'simplename', title: '简称',        minWidth: 100},
                    {field: 'address',    title: '校址'},
                    {field: 'tel',        title: '联系方式',     minWidth: 165},
                    {title: '操作', toolbar: '#college-option', minWidth: 140}
                ]]
            });
        }

        function getQueryParams(flag) {
            let params = $searchForm.serializeJson();
            return params;
        }

        function deleteColleges(collegeIds) {
            febs.get(ctx + 'college/delete/' + collegeIds, null, function () {
                febs.alert.success('删除院校成功');
                $query.click();
            });
        }

        //按回车搜索
        $(".layui-input").keyup(function (e) {//当按下按键时
            if (e.which == 13) {//.which属性判断按下的是哪个键，回车键的键位序号为13
                $query.click();
            }
        });
    })
</script>